/* 
 * Copyright 2014 ShangDao.Ltd  All rights reserved.
 * SiChuan ShangDao.Ltd PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @BaListActivity.java  2014-9-9 上午10:48:55 - Carson
 * @author YanXu
 * @email:981385016@qq.com
 * @version 1.0
 */

package com.android.tiango.baba;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.android.scsd.tiango.R;
import com.android.tiango.baba.BaListFragment.ListAdapter;
import com.android.tiango.baba.util.Constants;
import com.android.tiango.baba.util.ImageLoader;
import com.android.tiango.baba.util.StringUtils;
import com.android.tiango.baba.view.AppTitleBar;
import com.android.tiango.baba.view.PullListView;

public abstract class BaListActivity<T> extends BaBaseActivity {
	protected List<T> mListData = new ArrayList<T>();
	protected PullListView mListView;
	protected PullListView emptyListview;
	protected ListAdapter mAdapter;
	protected int mPageIndex = 1;
	protected ImageLoader mImageLoader;
	private View emptyFooter;
	private TextView emptyTextView;
	private String emptyContent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_listview);
        initEmptyView();		
		mImageLoader = new ImageLoader(this, R.drawable.ic_default);
		mAdapter = new ListAdapter(this);
		mTitleBar = (AppTitleBar) findViewById(R.id.titlebar);
		mListView = (PullListView) findViewById(R.id.id_pull_listview);
		mListView.setHeaderDividersEnabled(false);
		mListView.setFooterDividersEnabled(false);
		mListView.setSelector(android.R.color.transparent);
		if (null != addHeaderViewCallBack) {
			addHeaderViewCallBack.addHeaderView();
		}
		mListView.setAdapter(mAdapter);
	}

	protected abstract View getItemView(int position, View convertView, ViewGroup parent);

	public class ListAdapter extends SimpleAdapter {

		public ListAdapter(Context context) {
			super(context, null, 0, null, null);
		}

		public void putData(List<T> data) {
			if (mPageIndex == 1) {
				mListData.clear();
			}
			if (null != data) {
				mListData.addAll(data);
				if (data.size() < Constants.PAGE_SIZE) {
					mListView.setPullLoadEnable(false);
				} else {
					mListView.setPullLoadEnable(true);
				}
				 checkEmpty();
			}
			mAdapter.notifyDataSetChanged();
		}

		public void putData(List<T> data, int count) {
			if (mPageIndex == 1) {
				mListData.clear();
			}
			if (null != data) {
				mListData.addAll(data);
				if (mListData.size() < count) {
					mListView.setPullLoadEnable(true);
				} else {
					mListView.setPullLoadEnable(false);
				}
				checkEmpty();
			}
			mAdapter.notifyDataSetChanged();
		}

		public void clearData() {
			mListData.clear();
			mListView.setPullLoadEnable(false);
			mAdapter.notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			return mListData.size();
		}

		@Override
		public T getItem(int position) {
			if (position > -1 && position < mListData.size()) {
				return mListData.get(position);
			}
			return null;
		}

		@Override
		public boolean isEnabled(int position) {
			return true;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			return getItemView(position, convertView, parent);
		}
	}

	public interface AddHeaderViewCallBack {
		public void addHeaderView();
	}

	public AddHeaderViewCallBack addHeaderViewCallBack;

	protected void setHeaderView(AddHeaderViewCallBack addHeaderViewCallBack) {
		this.addHeaderViewCallBack = addHeaderViewCallBack;
	}

	private void initEmptyView() {
		emptyFooter = LayoutInflater.from(this).inflate(R.layout.layout_empty, null);
		emptyTextView = (TextView) emptyFooter.findViewById(R.id.tv_msg);
		emptyListview = (PullListView) findViewById(R.id.id_empty_listview);
		emptyListview.addHeaderView(emptyFooter);
		mAdapter = new ListAdapter(this);
		emptyListview.setAdapter(mAdapter);
		
		emptyTextView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

			}
		});
	}

	private void addEmptyHeader() {
		emptyListview = (PullListView) findViewById(R.id.id_empty_listview);
		mListView.setVisibility(View.GONE);
		emptyListview.setVisibility(View.VISIBLE);
		if (!StringUtils.isEmpty(emptyContent)) {
			emptyTextView.setText(emptyContent);
		}
	}

	protected void setEmptyViewMsg(String msg) {
		this.emptyContent = msg;
		emptyListview.startOnRefresh();
		emptyListview.onRefreshFinish();
		emptyListview.onLoadFinish();
	}

	private void checkEmpty() {
		if (!StringUtils.isEmpty(emptyContent)) {
			if (mPageIndex == 1 && mListData.size() < 1) {
				addEmptyHeader();
			} else {
				mListView.setVisibility(View.VISIBLE);
				emptyListview.setVisibility(View.GONE);
			}
			emptyListview.onRefreshFinish();
			emptyListview.onLoadFinish();
		}
	}
}
